package yxy.game.pm2.module.hitplanegame;

import org.joda.time.DateTime;
import yxy.apple.util.ActiveUtil;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;


//飞机
public class HitPlaneGameModule extends PlayerModule {

    // bean===========================================================================
    private int currentId;//当前关
    private DateTime updateTime;
    private int times; //当天挑战次数
    private ArrayList<Integer> rewardList = new ArrayList<Integer>();

    public ArrayList<Integer> getRewardList() {
        return rewardList;
    }

    public void setRewardList(ArrayList<Integer> rewardList) {
        this.rewardList = rewardList;
    }

    public int getTimes() {
        return times;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    public int getCurrentId() {
        return currentId;
    }

    public void setCurrentId(int currentId) {
        this.currentId = currentId;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    // func===========================================================================

    public static HitPlaneGameModule create(String playerCode) {
        HitPlaneGameModule module = new HitPlaneGameModule();
        module.setCurrentId(1);
        module.setUpdateTime(DateTime.now());
        module.setPlayerCode(playerCode);
        module.setTimes(0);
        module.set();
        return module;
    }

    public void reset(String playerCode){
        create(playerCode);
    }

    // redis==========================================================================

    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:HITPLANE", playerCode);
    }


    public void set() {
        try {
            setv(key(getPlayerCode()), this);
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public HitPlaneGameModule get(String playerCode) {
        try {
            HitPlaneGameModule module = getv(key(playerCode));
            //todo 后期放数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), HitPlaneGameModule.class);
                if (module != null) {
                    module.set();
                }//把数据同步到redis
            }
            if (module == null) {
                module = create(playerCode);
            }

            module.update();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 隔天刷新挑战次数
     */
    public void update() {
        //更新天数(转天更新)
        DateTime now = DateTime.now();
        int days = ActiveUtil.days(getUpdateTime(), now);
        if (days >= 1) {
            setTimes(0);
            setUpdateTime(now);
            set();
        }
    }


    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                HitPlaneGameModule module = getv(key(playerCode));
                if (module != null) MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }
}
